package com.example.baguwen.controller;

import com.example.baguwen.common.Result;
import com.example.baguwen.dto.UserLoginDTO;
import com.example.baguwen.dto.UserRegisterDTO;
import com.example.baguwen.service.UserService;
import com.example.baguwen.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @PostMapping("/register")
    public Result<UserVO> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        UserVO userVO = userService.register(registerDTO);
        return Result.success("注册成功", userVO);
    }
    
    @PostMapping("/login")
    public Result<UserVO> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        UserVO userVO = userService.login(loginDTO);
        return Result.success("登录成功", userVO);
    }
    
    @GetMapping("/current")
    public Result<UserVO> getCurrentUser(@RequestHeader("Authorization") String token) {
        // 处理Bearer token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        UserVO userVO = userService.getCurrentUser(token);
        return Result.success(userVO);
    }
    
    @GetMapping("/check-username")
    public Result<Boolean> checkUsername(@RequestParam String username) {
        boolean available = userService.isUsernameAvailable(username);
        return Result.success(available);
    }
} 